/**
 * 日期时间转换为：刚刚、*分钟前、*小时前、*天前、*周前、*个月前
 */

const minute = 1000 * 60;
const hour = minute * 60;
const day = hour * 24;
const halfamonth = day * 15;
const month = day * 30;

const fillZoon = function (vel) {
  return vel < 10 ? "0" + vel : vel;
};

module.exports = function getDate(dateTimeStamp, serveTimeStamp) {
  if (dateTimeStamp == undefined) return "";

  //时间戳
  if (
    typeof dateTimeStamp === "number" ||
    (typeof dateTimeStamp === "string" &&
      Number(dateTimeStamp) == dateTimeStamp)
  ) {
    dateTimeStamp = Number(dateTimeStamp);
    if (dateTimeStamp.toString().length < 13) dateTimeStamp *= 1000;
  }
  //字符串日期时间
  else if (typeof dateTimeStamp === "string") {
    dateTimeStamp = dateTimeStamp.replace(/\-/g, "/");
  }

  var sTime = new Date(dateTimeStamp).getTime();
  var now = serveTimeStamp
    ? new Date(serveTimeStamp).getTime()
    : new Date().getTime();
  var diffValue = now - sTime;

  if (diffValue < 0) {
    const date = new Date(dateTimeStamp);
    return `${fillZoon(date.getHours())}:${fillZoon(date.getMinutes())}`;
  }

  var monthC = diffValue / month;
  var weekC = diffValue / (7 * day);
  var dayC = diffValue / day;
  var hourC = diffValue / hour;
  var minC = diffValue / minute;

  if (monthC >= 1) {
    return parseInt(monthC) + "个月前";
  } else if (weekC >= 1) {
    return parseInt(weekC) + "周前";
  } else if (dayC >= 1) {
    return parseInt(dayC) + "天前";
  } else if (hourC >= 1) {
    return parseInt(hourC) + "个小时前";
  } else if (minC >= 5) {
    return parseInt(minC) + "分钟前";
  } else if (minC >= 3) {
    return "刚刚";
  } else {
    return "";
  }
};
